package mvp.com.register.view.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import mvp.com.register.R;
import mvp.com.register.presenter.UserRegisterPresenter;
import mvp.com.register.view.IRegisterView;

public class RegisterActivity extends Activity implements IRegisterView {

    UserRegisterPresenter mRegisterPresenter = new UserRegisterPresenter(this);

    private EditText et_username;
    private EditText et_password;
    private Button btn_reg;
    private Button btn_cle;
    private ProgressBar pb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
    }

    private void initView() {
        et_username = (EditText) findViewById(R.id.username);
        et_password = (EditText) findViewById(R.id.password);
        btn_reg = (Button) findViewById(R.id.reg);
        btn_cle = (Button) findViewById(R.id.cle);
        pb = (ProgressBar) findViewById(R.id.pb);

        btn_reg.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                mRegisterPresenter.register();
            }
        });

        btn_cle.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                mRegisterPresenter.clear();
            }
        });
    }

    @Override
    public void showProgressBar() {
        pb.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgressBar() {
        pb.setVisibility(View.GONE);
    }

    @Override
    public String getUsername() {
        return et_username.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return et_password.getText().toString().trim();
    }

    @Override
    public void showFailedError() {
        Toast.makeText(this, "注册失败了！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void toMainActivity() {
        Toast.makeText(this, "注册成功了！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void clearUsername() {
        et_username.setText("");
    }

    @Override
    public void clearPassword() {
        et_password.setText("");
    }
}
